﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.ComponentModel;
using System.Globalization;

namespace remes.BLoc
{
   public class TranslationEntry : INotifyPropertyChanged
   {
      public TranslationEntry(TranslationSet set, string streamName, string key)
      {
         TranslationSet = set;
         StreamName = streamName;
         Key = key;

         OriginalLanguage = null;
         Category = String.Empty;
         IsReadable = true;
         IsModifiable = true;
         Comments = String.Empty;
         OriginalText = String.Empty;

         Texts = new Dictionary<string, TranslationText>();
      }

      internal TranslationEntry(TranslationSet set, XElement xEntry)
      {
         TranslationSet = set;
         StreamName = (string)xEntry.Attribute("StreamName");
         Key = (string)xEntry.Attribute("Key");
         OriginalLanguage = (string)xEntry.Attribute("OriginalLanguage");
         Category = (string)xEntry.Attribute("Category");
         IsReadable = Boolean.Parse((string)xEntry.Attribute("IsReadable"));
         IsModifiable = Boolean.Parse((string)xEntry.Attribute("IsModifiable"));
         Comments = (string)xEntry.Attribute("Comments");
         OriginalText = (string)xEntry.Attribute("OriginalText");
         IsDeleted = Boolean.Parse((string)xEntry.Attribute("IsDeleted") ?? "False");

         Texts = new Dictionary<string, TranslationText>();
         foreach (var xText in xEntry.Elements("Text"))
         {
            var lang = TranslationSet.AddTargetLanguage((string)xText.Attribute("Language"));
            Texts.Add(lang, new TranslationText(this, xText));
         }
      }

      public TranslationSet TranslationSet { get; private set; }

      public string StreamName { get; private set; }

      public string Key { get; private set; }

      public string OriginalLanguage { get; internal set; }

      public string Category { get; internal set; }

      public bool IsReadable { get; internal set; }

      public bool IsModifiable { get; internal set; }

      public bool IsTextEmpty { get; private set; }

      public bool IsTextNumber { get; private set; }

      public string Comments { get; internal set; }

      private string _OriginalText;

      public string OriginalText
      {
         get { return _OriginalText; }
         internal set
         {
            _OriginalText = value;
            IsTextEmpty = String.IsNullOrWhiteSpace(_OriginalText);
            double dummy;
            IsTextNumber = _OriginalText != null && Double.TryParse(_OriginalText, out dummy);
         }
      }

      public bool IsDeleted { get; internal set; }

      public IDictionary<string, TranslationText> Texts { get; private set; }

      internal void SelectedLangChanged()
      {
         NotifyPropertyChnaged("SelectedText");
      }

      public TranslationText SelectedText
      {
         get
         {
            if (!String.IsNullOrEmpty(TranslationSet.SelectedLang))
            {
               return this[TranslationSet.SelectedLang];
            }
            return null;
         }
      }

      public TranslationText this[string lang]
      {
         get
         {
            TranslationText text;
            if (!Texts.TryGetValue(lang, out text))
            {
               text = new TranslationText(this, lang);
               Texts.Add(lang, text);
            }
            return text;
         }
      }

      internal void Save(XElement xEntry)
      {
         xEntry.Add(
            new XAttribute("StreamName", StreamName ?? String.Empty),
            new XAttribute("Key", Key ?? String.Empty),
            new XAttribute("OriginalLanguage", OriginalLanguage ?? String.Empty),
            new XAttribute("Category", Category ?? String.Empty),
            new XAttribute("IsReadable", IsReadable.ToString()),
            new XAttribute("IsModifiable", IsModifiable.ToString()),
            new XAttribute("Comments", Comments ?? String.Empty),
            new XAttribute("OriginalText", OriginalText ?? String.Empty),
            new XAttribute("IsDeleted", IsDeleted.ToString()));

         var originalCulture = CultureInfo.GetCultureInfo(OriginalLanguage);

         foreach (var text in Texts.Values)
         {
            // don't save a text with no real data in it
            if (!text.IsRedundant())
            {
               var xText = new XElement("Text");
               text.Save(xText);
               xEntry.Add(xText);
            }
         }
      }

      #region INotifyPropertyChanged Members

      protected void NotifyPropertyChnaged(string propertyName)
      {
         var temp = PropertyChanged;
         if (temp != null)
         {
            temp(this, new PropertyChangedEventArgs(propertyName));
         }
      }

      public event PropertyChangedEventHandler PropertyChanged;

      #endregion
   }
}
